package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.Nursecontent;
import com.neuedu.yyzx.service.NursecontentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursecontent")
public class NurseContentController {
    @Autowired
    private NursecontentService nursecontentService;

    @GetMapping("/list")
    public Ret list() {
        return Ret.ok(nursecontentService.list());
    }

    @GetMapping("/page")
    public Ret<List<Nursecontent>> page(Page<Nursecontent> page, Nursecontent nursecontent) {
        LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();
        if (nursecontent.getNursingName() != null && !"".equals(nursecontent.getNursingName())) {
            queryWrapper.like(Nursecontent::getNursingName, nursecontent.getNursingName());
        }
        queryWrapper.orderByDesc(Nursecontent::getId);
        page = nursecontentService.page(page, queryWrapper);
        return Ret.ok(page);
    }

    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody Nursecontent nursecontent) {
        boolean result = nursecontentService.saveOrUpdate(nursecontent);
        return Ret.ok(result);
    }

    @GetMapping("/getById/{id}")
    public Ret<Nursecontent> getById(@PathVariable int id) {
        Nursecontent result = nursecontentService.getById(id);
        return Ret.ok(result);
    }

    @GetMapping("/getByName/{name}")
    public Ret<Page<Nursecontent>> getByName(@PathVariable String name, Page<Nursecontent> page) {
        System.out.println(name);
        LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Nursecontent::getNursingName, name);
        queryWrapper.orderByDesc(Nursecontent::getId);
        Page<Nursecontent> resultPage = nursecontentService.page(page, queryWrapper);
        return Ret.ok(resultPage);
    }

    @DeleteMapping("/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        boolean result = nursecontentService.removeById(id);
        return Ret.ok(result);
    }
}
